/**
 * 
 */
package com.huawei.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.huawei.struts.form.CommonForm;
import com.huawei.struts.service.ICommonService;
import com.st.model.LjwPremissionT;

/**
 * @author Administrator
 * 
 */
public class CommonAction extends BaseAction {

	private final String returnUrl="/role.do?method=toRoleGrant";
	private ICommonService commonService;
	
	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	/**
	 * 初始化页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonForm commonForm =(CommonForm)form;
		return mapping.findForward("roleGrant");
	}
	/**
	 * 同步权限
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward synchronPermission(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonForm commonForm =(CommonForm)form;
		Integer id=new Integer(request.getParameter("id"));
		String[][] functionResource={
				{"channel","小区频道管理"},
				{"role","角色管理"},
				{"user","用户管理"},
				{"program","维度管理"},
				{"permission","权限管理"},
				{"property","数据字典管理"},
				{"lookup","基础数据管理"},
				{"forum","论坛管理"},
				{"album","相册管理"},
				{"attachment","附件管理"},
				{"catalog","栏目管理"},
				{"friend","好友管理"},
				{"goods","商品管理"},
				{"letter","私信管理"},
				{"village","小区管理"},
				{"activity","活动管理"},
				{"home","首页管理"}
				};
		String[][] operatorResource = { 
				{ "Read", "读取数据" },
				{ "Create", "创建数据" }, 
				{ "ReadAll ", "读取数据列表" },
				{ "Delete", "删除数据" },
				{ "Update", "修改数据" },
				{ "Grant", "授权" },
				{"Top","置顶"},
				{"CancelTop","取消置顶"},
				{"Glory","加精"}
				};
		//循环遍历权限 添加到数据库中
		List<LjwPremissionT> list=new ArrayList<LjwPremissionT>();
		for(int i=0;i<functionResource.length;i++){
			for(int j=0;j<operatorResource.length;j++){
				LjwPremissionT premission=new LjwPremissionT();
				premission.setRUid(functionResource[i][0]);
				premission.setRName(functionResource[i][1]);
				premission.setPrUid(operatorResource[j][0]);
				premission.setPrName(operatorResource[j][1]);
				list.add(premission);
			} 
		}
		commonService.synchronPermission(list);
		return super.goSuccess("操作成功", returnUrl+"&id="+id, request, response, mapping);
	}

}
